import 'package:flutter/material.dart';
import 'package:flutternettest/global/application.dart';
import 'package:flutternettest/global/routers.dart';
import 'package:flutternettest/utils/sp_util.dart';
import 'package:flutternettest/views/indicator.dart';

class WelcomePage extends StatefulWidget {
  @override
  _WelcomePageState createState() => _WelcomePageState();
}

class _WelcomePageState extends State<WelcomePage> {
  List<String> imagePaths = [
    'assets/images/guide1.png',
    'assets/images/guide2.png',
    'assets/images/guide3.png'
  ];

  int currentPosition = 0;

  var _pageController = PageController();

  @override
  Widget build(BuildContext context) {
    return Container(
        color: Color(Application.mColors.backgroundColor),
        child: Stack(
          children: <Widget>[
            PageView.builder(
              itemBuilder: (BuildContext context, int index) {
                return Image.asset(
                  imagePaths[index],
                  fit: BoxFit.fill,
                  width: MediaQuery.of(context).size.width,
                  height: MediaQuery.of(context).size.height,
                );
              },
              itemCount: 3,
              onPageChanged: _pageChange,
              controller: _pageController,
            ),
            Container(
              alignment: Alignment.bottomLeft,
              margin: EdgeInsets.only(bottom: 100.0, left: 25.0),
              child: currentPosition == 2
                  ? GestureDetector(
                      child: Container(
                        padding: EdgeInsets.only(
                            left: 20, right: 20, top: 5, bottom: 5),
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(50)),
                            color: Theme.of(context).primaryColor,
                            gradient: LinearGradient(
                              colors: [
                                Color(Application.mColors.primaryColor),
                                Color(Application.mColors.secondColor)
                              ],
                            )),
                        child: Text(
                          "进入应用",
                          style: TextStyle(
                              fontSize: Application.mDimens.commonSize,
                              color: Color(
                                  Application.mColors.textColorWithPrimary),
                              fontStyle: null,
                              decoration: TextDecoration.none),
                        ),
                      ),
                      onTap: () {
                        // 利用key来获取widget的状态FormState,可以用过FormState对Form的子孙FromField进行统一的操作
                        setShowWelcome(false);
                        Application.mRouter.navigateTo(
                            context, Routers.loginPagePath,
                            clearStack: true);
                      },
                    )
                  : null,
            ),
            Container(
                alignment: Alignment.bottomCenter,
                margin: EdgeInsets.only(bottom: 30.0),
                child: Indicator(_pageController, 3))
          ],
        ));
  }

  @override
  void initState() {
    super.initState();
    checkShow();
  }

  void checkShow() async {
    SPUtil su = await SPUtil.getInstance();
    bool showWelcome = su.getBool(SPUtil.keyShowWelcome);
    if (showWelcome != null && !showWelcome) {
      Application.mRouter
          .navigateTo(context, Routers.loginPagePath, clearStack: true);
    }
  }

  void _pageChange(int position) {
    setState(() {
      currentPosition = position;
    });
  }

  void setShowWelcome(bool show) async {
    SPUtil su = await SPUtil.getInstance();
    su.putBool(SPUtil.keyShowWelcome, show);
  }
}
